package com.atguigu.lease.common.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author Marco
 * @Description
 * @date 2025/8/7 1:02
 * @email 3293336923@qq.com
 */

// 因为  web-admin 中没有引入 阿里云，如果不加这个注解就会报错
//@ConditionalOnProperty(name = "aliyun.sms.endpoint")  作用: 如果配置了 aliyun.sms.endpoint 属性，才会将当前 AliyunSMSConfiguration 对象注入到IOC 容器中
//
@ConditionalOnProperty(name = "aliyun.sms.endpoint")
@Configuration
@EnableConfigurationProperties(AliyunSMSProperties.class)
public class AliyunSMSConfiguration {

    @Autowired
    private  AliyunSMSProperties aliyunSMSProperties;

    @Bean
    public Client createClient() {
        System.out.println("=====================");
        try {
            Config config = new Config();
            config.setAccessKeyId(aliyunSMSProperties.getAccessKeyId());
            config.setAccessKeySecret(aliyunSMSProperties.getAccessKeySecret());
            config.setEndpoint(aliyunSMSProperties.getEndpoint());
            return new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
